package com.xyyl.patient.core.util;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestClientException;
import org.springframework.web.client.RestTemplate;

import com.alibaba.fastjson.JSON;

/**
 * @projectName:xyyl-patient-web
 * @ClassName: RestClient
 * @Description: restClient
 * @author chen
 * @date 2017年9月28日
 */
public class RestClient {

	private static RestTemplate restTemplate;

	private final static String APPLICATION_JSON_UTF8_VALUE = "application/json;charset=UTF-8";

	static {
		if (restTemplate == null) {
			restTemplate = new RestTemplate();
			List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
			messageConverters.add(new StringHttpMessageConverter());
			messageConverters.add(new ByteArrayHttpMessageConverter());
			messageConverters.add(new FormHttpMessageConverter());
			restTemplate.setMessageConverters(messageConverters);
		}

	}

	/**
	 * 方法名称: postForResult 方法描述: 提交请求，返回String 参数类型：String;
	 * MultiValueMap<String, Object> 返回类型: String @throws
	 */
	public static String postForObject(String url, MultiValueMap<String, Object> multiValueMap)
			throws RestClientException {
		return restTemplate.postForObject(url, multiValueMap, String.class);
	}

	/**
	 * 方法名称: postUrl 方法描述: 含参数请求，返回String 参数类型：String;Object[] 返回类型:
	 * String @throws
	 */
	public static String postUrl(String url, Object paras[]) {
		return restTemplate.postForObject(url, null, String.class, paras);
	}

	public static <T> T postForObject(String url, Map<String, Object> multiValueMap, Class<T> claz)
			throws RestClientException {
		return restTemplate.postForObject(url, getDefaultFormEntity(multiValueMap), claz);
	}

	public static <T> T postForObject(String url, String json, Class<T> claz) throws RestClientException {
		return restTemplate.postForObject(url, getFormEntity(json), claz);
	}

	public static HttpEntity<String> getDefaultFormEntity(Map<String, Object> multiValueMap) {

		HttpHeaders headers = new HttpHeaders();

		/** 设置参数格式 **/
		MediaType type = MediaType.parseMediaType(APPLICATION_JSON_UTF8_VALUE);

		headers.setContentType(type);

		/** 设置响应格式 **/
		headers.add("Accept", APPLICATION_JSON_UTF8_VALUE);

		return new HttpEntity<String>(JSON.toJSONString(multiValueMap), headers);
	}

	public static HttpEntity<String> getFormEntity(String json) {

		HttpHeaders headers = new HttpHeaders();

		/** 设置参数格式 **/
		MediaType type = MediaType.parseMediaType(APPLICATION_JSON_UTF8_VALUE);

		headers.setContentType(type);

		/** 设置响应格式 **/
		headers.add("Accept", APPLICATION_JSON_UTF8_VALUE);

		return new HttpEntity<String>(json, headers);
	}

}
